
package com.ctyun.xstore.s3.api.object;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.AccessControlList;
import com.amazonaws.services.s3.model.GroupGrantee;
import com.amazonaws.services.s3.model.Permission;
import com.ctyun.xstore.s3.client.AmazonS3ClientBuilder;

public class SetObjectAcl {
    public static void setObjectAcl(String bucketName, String keyName) {
        AmazonS3 s3 = AmazonS3ClientBuilder.buildAmazonS3Client();
        try {
            // get the current ACL
            AccessControlList acl = s3.getObjectAcl(bucketName, keyName);
            // 给所有用户授予读取权限
            acl.grantPermission(GroupGrantee.AllUsers, Permission.Read);
            s3.setObjectAcl(bucketName, keyName, acl);
        }catch (AmazonServiceException e) {
            System.err.println(e.getErrorMessage());
        }
        System.out.println("Done!");
    }

}
